package com.liang.lollipop.lreader.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.IBinder;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.support.annotation.RequiresApi;

import com.liang.lollipop.lreader.R;
import com.liang.lollipop.lreader.bean.ModelBean;
import com.liang.lollipop.lreader.util.ModelDAO;
import com.liang.lollipop.lreader.util.Settings;
import com.liang.lollipop.lreader.util.ToastUtil;

/**
 * Created by LiuJ on 2017/07/06.
 * 状态栏快速设置的服务
 */

@RequiresApi(api = Build.VERSION_CODES.N)
public class QuickSettingService extends TileService {

//    private int toggleState = STATE_OFF;

    private ModelDAO modelDAO;

    private void updateTileState(int type){
//        toggleState = type;
        Icon icon;
        if(type==MaskingService.TYPE_OFF){
            icon = Icon.createWithResource(getApplicationContext(), R.drawable.ic_visibility_off_white_24dp);
            getQsTile().setState(Tile.STATE_INACTIVE);// 更改成非活跃状态

        }else{
            icon = Icon.createWithResource(getApplicationContext(), R.drawable.ic_visibility_white_24dp);
            getQsTile().setState(Tile.STATE_ACTIVE);//更改成活跃状态
        }
        getQsTile().setIcon(icon);//设置图标
        getQsTile().updateTile();//更新Tile
    }

    private void T(String str){
        ToastUtil.T(this,str);
    }



    @Override
    public void onTileAdded() {
        super.onTileAdded();
        T("守护者已置入手牌");
    }

    @Override
    public void onTileRemoved() {
        super.onTileRemoved();
        T("守护者已放回牌库");
        updateTileState(MaskingService.TYPE_OFF);
    }

    @Override
    public void onStartListening() {
        super.onStartListening();
        //打开下拉菜单的时候调用,当快速设置按钮并没有在编辑栏拖到设置栏中不会调用
        //在TileAdded之后会调用一次
        updateTileState(Settings.getState(this)?MaskingService.TYPE_ON:MaskingService.TYPE_OFF);
    }

    @Override
    public void onStopListening() {
        super.onStopListening();
        //关闭下拉菜单的时候调用,当快速设置按钮并没有在编辑栏拖到设置栏中不会调用
        // 在onTileRemoved移除之前也会调用移除
    }

    @Override
    public void onClick() {
        super.onClick();
//        updateTileState((toggleState+1)%2);
        Intent intent = new Intent(this,MaskingService.class);
        if(Settings.getState(this)){
            intent.putExtra(MaskingService.ARG_TYPE,MaskingService.TYPE_OFF);
        }else{
            ModelBean bean = modelDAO.selectFirst();
            intent.putExtra(MaskingService.ARG_TYPE,MaskingService.TYPE_ON);
            intent.putExtra(MaskingService.ARG_COLOR,bean.value);
            intent.putExtra(MaskingService.ARG_ID,bean.id);
        }
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            startForegroundService(intent);
        }else{
            startService(intent);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {

        return super.onBind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        filter.addAction(MaskingService.TILE_ACTION);
        registerReceiver(MaskingReceiver, filter);
        modelDAO = new ModelDAO(this);
    }

    private BroadcastReceiver MaskingReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateTileState(intent.getIntExtra(MaskingService.ARG_TILE_STATE,MaskingService.TYPE_OFF));
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(MaskingReceiver);
    }
}

